/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#include "EntityComponent.h"
#include "../XPropertys/PropertyFactory.h"

namespace XF
{
	int EntityComponent::smComponentSpecificID = 0;	

	EntityComponent::EntityComponent(GameEntity *owner, std::string idName)
		:mOwner(owner), mComponentName(idName), mInitialized(false), mKillComponent(false)
	{
		mComponentID = smComponentSpecificID;
		smComponentSpecificID++;
	}

	EntityComponent::~EntityComponent()
	{
		Shutdown();
	}

	void EntityComponent::CopyPropertys(EntityComponent *component)
	{
		RemoveAllProperties();

		for(std::map<std::string, IProperty*>::const_iterator it = component->mProperties.begin();
			it != component->mProperties.end(); ++it)
		{
			IProperty* newProp = PropertyFactory::CreateProperty(it->second->TypeID(), it->first, it->second->ToString());

			AddProperty(newProp);
		}
	}

	void EntityComponent::Initialize()
	{
		if(!OnInitialize())
		{
			ILOG << "[EntityComponent] Entity: " << mOwner->ID() << " Component: " << mComponentName << " failed to initialize - killing component. (Error message: " << mErrorMessage << ")";

			Kill();
		}
		else
		{
			mInitialized = true;
		}
	}
}

